package com.hspedu.socket;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/2
 * @time 16:31
 */
public class SocketTCP03Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务端开始监听...");
        Socket accept = serverSocket.accept();

        InputStream inputStream = accept.getInputStream();
        int readLen = 0;
        byte[] buf = new byte[1024];

        while ((readLen = inputStream.read(buf))!=-1){
            System.out.println(new String(buf,0,readLen));
        }

        OutputStream outputStream = accept.getOutputStream();
        outputStream.write("hello,client".getBytes());
        accept.shutdownOutput();
        outputStream.close();
        inputStream.close();
        accept.close();
        serverSocket.close();
    }
}
